<?php
/*
Plugin Name: Query Example
Plugin URI: http://cdiabu.com
Description: A plugin to demonstrate customized queries.
Version: 1.0
Author: gradyetc
Author URI: http://grady-etc.com
License: GPL2
*/

/**
 * This plugin demonstrates a custom post query using the powerful WP_Query class.
 * 
 * To view details about your query, download the Debug Bar Plugin (http://wordpress.org/extend/plugins/debug-bar/)
 * and add the following line to your wp-config.php file:
 * 
 * define( 'SAVEQUERIES', true);
 */

// WordPress Hooks
add_action( 'init', 'qe_init' );
add_action( 'admin_menu', 'qe_add_menus' );

register_activation_hook( __FILE__, 'qe_on_activate' );

/**
 * Activation hook
 *
 * Run init callback (register post types and taxonomy) and then flush the rewrite rules
 * 
 */
function qe_on_activate() {
	
	// Register post type(s) and taxonomy
	qe_init();
	
	// Flush rewrite cache
	flush_rewrite_rules();

}

/**
 * You MUST register custom post types and taxonomy in the init callback
 *
 */
function qe_init() {
	
	$genre_args = array( 
		'hierarchical' => true,
		'query_var' => 'genre',
		'rewrite' => array(
			'slug' => 'works/by-genre',
			'with_front' => false,
		),
		'labels' => array(
			'name' => 'Genres',
			'singular_name' => 'Genre'
		)
	);
	
	// Register taxonomy first
	register_taxonomy( 'genre', array( 'work' ), $genre_args );
	
	$work_args = array(
		'public' => true,
		'query_var' => 'work',
		'rewrite' => array(
			'slug' => 'works',
			'with_front' => false
		),
		'has_archive' => true,
		'supports' => array(
			'title',
			'thumbnail',
			'editor',
		),
		'labels' => array(
			'name' => 'Works',
			'singular_name' => 'Work',
		)
	);
	
	// Register post type second
	register_post_type( 'work', $work_args );
	
}

/**
 * Add a temporary options page to demonstrate a custom query loop
 */
function qe_add_menus() {

	// Add an options page for performing our query
	add_options_page( 
		'Query Example Page', // Page title
		'Query Settings', // Menu title
		'manage_options', // Menu permissions
		'query-example', // unique menu slug
		'qe_query_example_page' 
		);
}

/**
 * Render the temporary options page to query our custom posts
 */
function qe_query_example_page() {
	
	// Double checking our user permissions
	if( !current_user_can('manage_options')) {
		wp_die("Cheatin', uh?");
	}

	// Options page div.wrap and title
	echo '<div class="wrap">';
	echo '<h1>Query Example Page</h1>';

	/**
	 * WP_Query is the monster class which WordPress uses behind the scenes
	 * to create all of the SQL queries to content from the database
	 * 
	 * Please see the class reference for more information:
	 * http://codex.wordpress.org/Class_Reference/WP_Query
	 */

	$work_query_args = array(
	    'post_type' => 'work',		// could be post, page, or any
	    'post_status' => 'publish',		// could be draft, pending, future, or publish
	    'posts_per_page' => 5,		// -1 returns all posts, but we could easily set a number
	    'orderby' => 'title',		// many other values available (see the codex)
	    'order' => 'ASC',			// or DESC
	    //'genre' => 'country',		// Specify a taxonomy and term for which to query posts by
	);

	// This statements creates a new instance of the WP_Query class, which in turn runs
	// the query based on the provided arguments
	$work_query = new WP_Query( $work_query_args );

	// Custom query loop
	if( $work_query->have_posts() ) {
		
		echo '<h2>Edit Works</h2>';
		echo '<ol>';
		
		while( $work_query->have_posts() ) {

			// Populate global post with current result (enable loop template tags)
			$work_query->the_post();

			/* Format the results as you would like here */
			echo '<li><p>';
			
			// Display the post title wrapped in a link to edit the post
			printf( '<a href="%s" >%s</a> ', get_edit_post_link(), get_the_title() );
			
			// Display the post terms for the 'genre' taxonomy, surrouned by paranthesis
			the_terms( get_the_ID(), 'genre', '(', ', ', ')' );
			
			echo '</p></li>';
		}

		echo '</ol>';
	}

	// This resets WordPress's global post array, clearing out the results
	// you just queried and preparing it for the query
	wp_reset_postdata();

	// Close options page div.wrap
	echo '</div>';
	
}

?>